﻿Imports GTA
Imports GTA.Native
Namespace OnlinePlayer
    Public Class PlayerChanger
        Inherits Script
        Private Shared originalPed As Ped
        Private Shared character As Ped
        Private Shared active As Boolean
        Public Sub New()
            Interval = 100
        End Sub

        Public Shared Sub ChangeTo(model As Model, style As Action(Of Ped))
            If active Then
                UI.Screen.ShowSubtitle("角色已切换")
                Return
            End If
            If Not OnlinePlayerOption.EnableMPMode Then
                UI.Screen.ShowSubtitle("需要先启用线上玩家技能")
                Return
            End If
            If Game.IsMissionActive OrElse Game.IsRandomEventActive Then
                UI.Screen.ShowSubtitle("无法在任务中切换角色")
                Return
            End If
            character = World.CreatePed(model, PlayerPed.Position.Around(1))
            If character IsNot Nothing Then '创建成功
                style(character)
                For Each weapon As WeaponHash In [Enum].GetValues(GetType(WeaponHash))
                    character.Weapons.Give(weapon, 9999, False, False)
                Next
                '保存原角色
                originalPed = PlayerPed
                originalPed.IsInvincible = True
                '切换新角色
                [Function].Call(Hash.CHANGE_PLAYER_PED, Game.Player, character, False, True)
                [Function].Call(Hash.SET_DIRECTOR_MODE_AVAILABLE, True)
                character.MaxHealth = 500
                character.Health = 500
                active = True
            Else
                UI.Screen.ShowSubtitle("创建失败")
            End If
        End Sub
        Public Shared Sub Reset()
            If active Then
                [Function].Call(Hash.SET_DIRECTOR_MODE_AVAILABLE, False)
                [Function].Call(Hash.CHANGE_PLAYER_PED, Game.Player, originalPed, False, True)
                character.Delete()
                originalPed.IsInvincible = False

                active = False
            End If
        End Sub

        Private Sub PlayerChanger_Tick(sender As Object, e As EventArgs) Handles Me.Tick
            If active Then
                If Not OnlinePlayerOption.EnableMPMode.Enabled OrElse Game.IsMissionActive OrElse Game.IsRandomEventActive Then
                    Reset()
                End If
            End If

        End Sub
    End Class
End Namespace

